﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BSC_OTC_Management.BPS_Services;

namespace BSC_OTC_Management.Forms
{
    public partial class Frm_PlaceOrder : Form
    {
        #region "KHAI BAO BIEN TOAN CUC"

        Code.CL_Ultility _cl = new Code.CL_Ultility();
        Code.StaticVariable _code = new Code.StaticVariable();

        public DataTable dtb_LenhFlex;

        public DataTable Get_LenhFlex
        {
            get { return dtb_LenhFlex; }
            set { dtb_LenhFlex = value; }
        }

        public string V_strLoaiLenh;

        public string Get_loaiLenh
        {
            get { return V_strLoaiLenh; }
            set { V_strLoaiLenh = value; }
        }

        #endregion

        #region "CAC HAM CHUNG CUA FORM"

        public void FAO_PLACEORDER(string pv_afacctno, string p_exectype, string pv_symbol, string pv_quantity, string pv_quoteprice, string pv_pricetype, string pv_tlid, int v_Solenh)
        {
            try
            {
                const string functionName = "PLACEORDER";
                const string moduleName = "Orders";

                string[] keyField =
                {
                    "p_functionname", "p_username", "p_acctno", "p_afacctno", "p_exectype", "p_symbol", "p_quantity",
                    "p_quoteprice", "p_pricetype", "p_timetype", "p_book", "p_via", "p_dealid", "p_direct",
                    "p_effdate", "p_expdate", "p_tlid", "p_quoteqtty", "p_limitprice", "p_err_code", "p_err_message"
                };

                string[] keyValue =
                {
                    "PLACEORDER", "", "", pv_afacctno,p_exectype, pv_symbol, pv_quantity, pv_quoteprice,pv_pricetype, "T", "A", "H", "",
                    "Y", "", "", pv_tlid, "0", "0", "", ""
                };
                string[] keyType =
                {
                    "VARCHAR2", "VARCHAR2", "VARCHAR2", "VARCHAR2", "VARCHAR2", "VARCHAR2", "NUMBER",
                    "NUMBER", "VARCHAR2", "VARCHAR2", "VARCHAR2", "VARCHAR2", "VARCHAR2", "VARCHAR2", "VARCHAR2", "VARCHAR2",
                    "VARCHAR2", "VARCHAR2", "VARCHAR2", "VARCHAR2", "VARCHAR2",
                };
                string[] keyDirection =
                {
                    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
                    "1", "1", "2", "2"
                };

                var bps = new OnlineTradingWcfClient();
                var result = bps.ExecBOFunction(functionName, moduleName, keyField, keyValue, keyType, keyDirection);

                string v_MaLoi = "";
                string V_DienGiai = "";
                foreach (var item in result)
                {
                    if (item.Key.Contains("p_err_code"))
                    {
                        v_MaLoi = item.Value;
                        V_DienGiai = _cl.LAY_THONG_TIN_LOI_TU_MA_LOI(v_MaLoi);
                    }
                }

                MessageBox.Show("[" + v_MaLoi + "] : " + V_DienGiai,"Thông báo lệnh số " + v_Solenh.ToString());
                
             if (Code.StaticVariable.V_BoolLOG)
                {
                    string v_Err = "pv_afacctno=" + pv_afacctno + ", p_exectype=" + p_exectype + ", pv_symbol=" + pv_symbol + ", pv_quantity=" + pv_quantity + ", pv_quoteprice=" + pv_quoteprice + ",pv_pricetype=" + pv_pricetype;
                _code._services.UPDATE_LOG_FILE("LOG",Code.StaticVariable.userID,"Đặt lệnh thành công","FAO_PLACEORDER",v_Err);
                }
              
            }
            catch (Exception ex)
            {
              _code._services.UPDATE_LOG_FILE("ERR",Code.StaticVariable.userID,"Lỗi trong quá trình đặt lệnh","FAO_PLACEORDER",ex.ToString());
            }

        }

        private void Du_lieu_confirmLenh()
        {
            try
            {
                DataTable TBL_CONFIRMLENH = new DataTable();

                DataColumn column;
                DataRow row;

                column = new DataColumn();
                column.DataType = System.Type.GetType("System.Int32");
                column.ColumnName = "STT";
                TBL_CONFIRMLENH.Columns.Add(column);


                column = new DataColumn();
                column.DataType = Type.GetType("System.String");
                column.ColumnName = "CUSTODYCD";
                TBL_CONFIRMLENH.Columns.Add(column);

                column = new DataColumn();
                column.DataType = Type.GetType("System.String");
                column.ColumnName = "SIDE";
                TBL_CONFIRMLENH.Columns.Add(column);


                column = new DataColumn();
                column.DataType = Type.GetType("System.String");
                column.ColumnName = "SYMBOL";
                TBL_CONFIRMLENH.Columns.Add(column);

                column = new DataColumn();
                column.DataType = Type.GetType("System.String");
                column.ColumnName = "QUANTITY";
                TBL_CONFIRMLENH.Columns.Add(column);


                column = new DataColumn();
                column.DataType = Type.GetType("System.String");
                column.ColumnName = "QUOTEPRICE";
                TBL_CONFIRMLENH.Columns.Add(column);

                int l = dtb_LenhFlex.Rows.Count;
                for (int i = 0; i < l; i++)
                {
                    row = TBL_CONFIRMLENH.NewRow();
                    row["STT"] = i + 1;
                    row["CUSTODYCD"] = dtb_LenhFlex.Rows[i]["CUSTODYCD"].ToString();
                    string v_Side = dtb_LenhFlex.Rows[i]["SIDE"].ToString();
                    if (v_Side == "B")
                    {
                        v_Side = "Mua";
                        this.BackColor = Color.FromArgb(167, 204, 240);
                    }
                    else
                    {
                        v_Side = "Bán";
                        this.BackColor = Color.FromArgb(245, 197, 217);
                    }
                    row["SIDE"] = v_Side;
                    row["SYMBOL"] = dtb_LenhFlex.Rows[i]["SYMBOL"].ToString();
                    row["QUANTITY"] = dtb_LenhFlex.Rows[i]["QUOTEQTTY"].ToString();
                    row["QUOTEPRICE"] = dtb_LenhFlex.Rows[i]["QUOTEPRICE"].ToString();
                    TBL_CONFIRMLENH.Rows.Add(row);
                }


                dataGridView1.DataSource = TBL_CONFIRMLENH;


            }
            catch (Exception ex)
            {
                _code._services.UPDATE_LOG_FILE("ERR", Code.StaticVariable.userID, "Lỗi trong load lệnh confirm", "Du_lieu_confirmLenh", ex.ToString());
            }

        }

        #endregion
        public Frm_PlaceOrder()
        {
            InitializeComponent();

        }
        private void Frm_PlaceOrder_Load(object sender, EventArgs e)
        {
            try
            {
                this.lbl_loaiLenh.Text = Get_loaiLenh;
                _cl.Get_PlaceOrder_Status = false;
                Du_lieu_confirmLenh();
            }
            catch (Exception ex)
            {
                _code._services.UPDATE_LOG_FILE("ERR", Code.StaticVariable.userID, "Lỗi trong load lệnh confirm", "Frm_PlaceOrder_Load", ex.ToString());
            }

        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            this.Close();
            _cl.Get_PlaceOrder_Status = false;
        }

        private void btn_XacNhan_Click(object sender, EventArgs e)
        {
            try
            {
                string pv_afacctno, pv_exectype, pv_symbol, pv_Side, pv_quantity, pv_quoteprice, pv_tlid;
                string pv_pricetype = Get_loaiLenh;
                int l = dtb_LenhFlex.Rows.Count;

                //------------------GHI LOG--------------
                if (Code.StaticVariable.V_BoolLOG)
                { _code._services.UPDATE_LOG_FILE("LOG", Code.StaticVariable.userID, "Bấm nút đặt lệnh", "btn_XacNhan_Click", "SO LENH SE DAT VAO FLEX " + l.ToString()); }
                //------------------

                int v_solenh = 0;
                for (int i = 0; i < l; i++)
                {
                    pv_afacctno = dtb_LenhFlex.Rows[i]["ACCTNO"].ToString();
                    v_solenh += 1;
                    pv_Side = dtb_LenhFlex.Rows[i]["SIDE"].ToString();

                    if (pv_Side == "B")
                    {
                        pv_exectype = "NB";
                    }
                    else
                    {
                        pv_exectype = "NS";
                    }

                    pv_symbol = dtb_LenhFlex.Rows[i]["SYMBOL"].ToString();
                    pv_quantity = dtb_LenhFlex.Rows[i]["QUOTEQTTY"].ToString().Replace(",", "");
                    pv_quoteprice = dtb_LenhFlex.Rows[i]["QUOTEPRICE"].ToString().Replace(",", "");
                    pv_quoteprice = (Convert.ToDouble(pv_quoteprice)/1000 ).ToString();
                    pv_tlid = "6868";

                    FAO_PLACEORDER(pv_afacctno, pv_exectype, pv_symbol, pv_quantity, pv_quoteprice, pv_pricetype, pv_tlid, v_solenh);
                    _cl.Get_PlaceOrder_Status = true;

                }

                this.Close();

            }
            catch (Exception ex)
            {
                _code._services.UPDATE_LOG_FILE("ERR", Code.StaticVariable.userID, "Lỗi bấm xác nhận lệnh", "btn_XacNhan_Click()", ex.ToString());
            }
        }
    }


}
